package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import jdbc.ConnectionFactory;
import Modelo.Pagamento;

public class PagamentoDAO {
	//
	// ATRIBUTOS
	//
	private Connection cnn;
	private PreparedStatement stmt;
	private String sql;

	//
	// CONSTRUTOR
	//
	public PagamentoDAO() {
		conectar();
	}

	//
	// INSERT
	//

	public void adicionar(Pagamento v) {
		this.sql = "INSERT INTO Pagamento (idLocacao, nome, cpf, numeroCar, dataValidade, codigoSeg, agencia," +
				   "conta, multa, dinheiro) VALUES (?,?,?,?,?,?,?,?,?,?)";
		conectar();

		try {
			stmt = cnn.prepareStatement(sql);
			
			stmt.setString(1, v.getIdLocacao());
			stmt.setString(2, v.getNome());
			stmt.setString(3, v.getCpf());
			stmt.setString(4, v.getNumeroCar());
			stmt.setString(5, v.getDataValid());
			stmt.setString(6, v.getCodigoSeg());
			stmt.setString(7, v.getAgencia());
			stmt.setString(8, v.getConta());
			stmt.setString(9, v.getMulta());
			stmt.setString(10, v.getDinheiro());
			
			stmt.execute();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				cnn.rollback();
			} catch (SQLException e1) {
				System.out.print(e1.getStackTrace());
			}
		} finally {
			fechar();
		}
	}

	//
	// CONECTAR
	//
	private void conectar() {
		this.cnn = new ConnectionFactory().getConnection();
	}

	//
	// FECHAR
	//
	private void fechar() {
		try {
			stmt.close();
			cnn.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
